﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : tian_
// Created          : 05-16-2018
//
// Last Modified By : tian_
// Last Modified On : 04-23-2018
// ***********************************************************************
// <copyright file="GroupModelRotateCmd.cs" company="">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************
using Osg;
using OSS.BIM.Business;
using OSS.BIM.Model;
using OSS.BIM.SceneManage;
using OSS.BIM.Utility;
using ZFGK.Addins;
using ZFGK.osgEarth.UI;
using ZFGK.osgEarth.Utility;

namespace OSS.BIM.Commands.GroupModelCmds
{
    /// <summary>
    /// Class GroupModelRotateCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class GroupModelRotateCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
//             if (!PermissionApp.CurUser.CheckUser())
//             {
//                 return CmdResult.Cancel;
            //             }
            var form = cmdData.ViewForm as OsgEarthViewForm;
            var osgEarthViewCtrl = form.OsgEarthViewCtrl;
            var actionUtility = osgEarthViewCtrl.ActionUtility;
            ModelElement userModel = cmdData.Tag as ModelElement;
            if (userModel == null)
            {
                if (!actionUtility.PickUserModel(out userModel))
                {
                    return CmdResult.Cancel;
                }
            }
            // 删除现在的模型
            osgEarthViewCtrl.OsgEarthObj.DeleteModel(userModel);
            //插入点
            Vec3d mapPt1 = userModel.GetLocation();
            //userModel.SetLocation(mapPt1);

            // 选择旋转点
            Vec3d mapPt2 = null;
            if (!actionUtility.PickEarthPointLastex("设置旋转点", mapPt1, userModel.GetScaleNode(), out mapPt2))
            {
                osgEarthViewCtrl.OsgEarthObj.AddOrReplaceModel(userModel);

                return CmdResult.Cancel;
            }
            userModel.Rotation = mapPt1.Rotation(mapPt2);
            if (DB.Context.Update(userModel) == 0)
            {
                return CmdResult.Cancel;
            }

            // 放置模型
            userModel.AddOrUpdateTreeNode(true);
            osgEarthViewCtrl.OsgEarthObj.AddOrReplaceModel(userModel);
            return CmdResult.Succeed;
        }
    }
}
